﻿using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;

namespace ZBJF.Warranty.BLL
{
    public partial class SellerEmployeeRoleService
    {
        public SellerEmployeeRole LoadByID(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        public bool IsExistRoleName(string roleName, long id = 0)
        {
            var expressionWhere = PredicateExtensions.True<SellerEmployeeRole>().And(o => o.RoleName.Equals(roleName));
            if (id > 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }
        public bool IsExistRoleCode(string roleCode, long id = 0)
        {
            var expressionWhere = PredicateExtensions.True<SellerEmployeeRole>().And(o => o.RoleCode.Equals(roleCode));
            if (id > 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }

       

        public IQueryable<SellerEmployeeRole> LoadAll()
        {
            return LoadEntities(o => o.IsAvailable);
        }

        public IQueryable<SellerEmployeeRole> GetPagingList(BaseQueryParams model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<SellerEmployeeRole>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                string kw = model.Keyword.ToLower();
                expressionWhere = expressionWhere.And(o => (o.RoleName + SqlFunctions.Space(1) + o.RoleCode).ToLower().Contains(kw));
            }
            return LoadPagerEntities<SellerEmployee>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
    }
}